package com.primaryedu.ui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.primaryedu.R;
import com.primaryedu.data.Contants;
import com.primaryedu.data.GlobalData;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.TextView;

/**
 * 网页
 *
 * @author jun.wang
 * @description
 * @data 2016年12月15日
 */
@SuppressLint("SetJavaScriptEnabled")
public class HtmlActivity extends BaseActivity implements OnClickListener {
    private WebView webView;
    private String titleStr, urlStr, noticeid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_html);

        titleStr = getIntent().getStringExtra("titleStr");
        urlStr = getIntent().getStringExtra("urlStr");
        noticeid = getIntent().getStringExtra("noticeid");
        init();
    }

    private void init() {
        findViewById(R.id.back).setOnClickListener(this);
        ((TextView) findViewById(R.id.title)).setText(titleStr);
        findViewById(R.id.right).setOnClickListener(this);

        webView = (WebView) this.findViewById(R.id.webView);
        webView.loadUrl(urlStr);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccess(true);

        // 设置可以支持缩放 
        settings.setSupportZoom(true);
        // 设置出现缩放工具 
        settings.setBuiltInZoomControls(true);
        //扩大比例的缩放
        settings.setUseWideViewPort(true);
        //自适应屏幕
        settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });

        if (titleStr.equals("通知详情")) {
            requestData();
        }
    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_html;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.back:
//			 if(webView.getUrl().equals("http://47.104.57.2:8080/a/login;JSESSIONID=075d566effe2452f94a5c70d7b4859f6")){
//				 finish();
//			 }else {
//				 webView.goBack();// 返回前一个页面
//			 }

                finish();
                break;

            default:
                break;
        }
    }

    private void requestData() {
        showLoading("加载中...");
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.saveRecord,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println("saveRecord==" + response);
                        dismissLoading();
                        try {
                            JSONObject json = new JSONObject(response);
                            int result = json.getInt("result");

                            if (result == 1) {
                                Intent intent = getIntent();
                                setResult(3, intent);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("noticeid", noticeid);
                map.put("userid", GlobalData.getUserId(getApplicationContext()));
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}